﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Dubs_Finder.Model
{
    public class MainModel
    {
        private CyclicalFileBuffer cb = new CyclicalFileBuffer(1024);
        public ObservableCollection<DubsWrapper> dw { get; private set; }
        private string directory = "C:\\Users\\Anders\\Documents\\Visual Studio 2012\\Projects\\Dubs Finder\\Dubs Finder\\test";

        public int ScannedFiles { get; internal set; }

        public delegate void updateScannedCounter(int count);
        public delegate void updateStateText(string state);
        updateScannedCounter usc;
        updateStateText ust;


        public MainModel()
        {
            this.dw = new AsyncObservableCollection<DubsWrapper>();
        }

        public void main()
        {
            try
            {
                Debug.WriteLine("Running MainModel");


                // Create a DirectoryInfo object representing the specified directory.
                DirectoryInfo dir = new DirectoryInfo(directory);

                CheckEm ce = new CheckEm(this.cb, this.dw, this, this.usc, this.ust);
                ce.runCheck(dir);
            }
            catch (UnauthorizedAccessException e)
            {
                Debug.WriteLine("Access denied");
                Debug.WriteLine(e.StackTrace);
            }
        }

        public void ParseScannedCounter(updateScannedCounter up)
        {
            this.usc = up;
        }

        public void ParseStateText(updateStateText up)
        {
            this.ust = up;
        }

        public bool GetDirectory()
        {
            /*FolderBrowserDialog fbd = new FolderBrowserDialog();
            DialogResult dr = fbd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                directory = fbd.SelectedPath;
                return true;
            }
            else
            {
                Console.WriteLine("No directory selected.");
                return false;
            }*/
            return true;
        }

        public void CleanUp()
        {
            this.ScannedFiles = 0;
            this.cb = new CyclicalFileBuffer(1024);
            this.dw.Clear();
        }

        public int GetTotalFiles()
        {
            int count = 0;
            try
            {
                DirectoryInfo dir = new DirectoryInfo(directory);
                DirectoryInfo[] subDir = dir.GetDirectories();
                foreach (DirectoryInfo dInfo in subDir)
                {
                    count += GetTotalFiles(dInfo);
                }

                FileInfo[] fi = dir.GetFiles();

                foreach (FileInfo fInfo in fi)
                {
                    ++count;
                }
            }
            catch (UnauthorizedAccessException e)
            {
                Debug.WriteLine("Access denied");
                Debug.WriteLine(e.StackTrace);
            }

            return count;
        }

        public int GetTotalFiles(DirectoryInfo di)
        {
            int count = 0;
            try
            {
                DirectoryInfo[] subDir = di.GetDirectories();
                foreach (DirectoryInfo dInfo in subDir)
                {
                    count += GetTotalFiles(dInfo);
                }

                FileInfo[] fi = di.GetFiles();

                foreach (FileInfo fInfo in fi)
                {
                    ++count;
                }
            }
            catch (UnauthorizedAccessException e)
            {
                Debug.WriteLine("Access denied");
                Debug.WriteLine(e.StackTrace);
            }
            return count;
        }
    }
}
